package com.fieldchain.ex.fa100.ui.personal.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.bean.BankChoseBean;
import com.fieldchain.ex.fa100.inter.StringCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/13
 */
public class BankChoseAdapter extends RecyclerView.Adapter<BankChoseAdapter.ViewHolder> {
    private Context mContext;
    private StringCallBack callBack;

    private boolean[] tags;
    private int oldIndex = -1, newIndex = 0;

    private String checkName = "";


    public void setCheckName(String checkName) {
        this.checkName = checkName;
    }

    public BankChoseAdapter(Context mContext, StringCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    private List<BankChoseBean> list = new ArrayList<>();

    public void setData(List<BankChoseBean> list) {
        this.list.clear();
        this.list.addAll(list);
        tags = new boolean[list.size()];
        for (int i=0;i<list.size();i++){
            if(list.get(i).getName().equals(checkName)){
                oldIndex=i;
                tags[oldIndex]=true;
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bankchaose, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final BankChoseBean bean = list.get(position);
        holder.tvName.setText(bean.getName());
        holder.cbox.setChecked(bean.getName().equals(checkName));
        holder.reaItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newIndex = position;
                tags[position] = !tags[position];

                if (newIndex != oldIndex) {
                    checkName = bean.getName();
                    if (oldIndex != -1) {
                        tags[oldIndex] = false;
                    }
                    notifyItemChanged(oldIndex);
                    oldIndex = newIndex;
                } else {
                    if (!tags[newIndex]) {
                        checkName = "";
                    }else {
                        checkName = bean.getName();
                    }
                }
                callBack.message(checkName);
                notifyItemChanged(newIndex);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout reaItem;
        private TextView tvName;
        private CheckBox cbox;

        public ViewHolder(View itemView) {
            super(itemView);
            reaItem = itemView.findViewById(R.id.rea_item);
            tvName = itemView.findViewById(R.id.tv_name);
            cbox = itemView.findViewById(R.id.cbox);
        }
    }
}
